package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.course.CourseDTO;
import com.qk.entity.Course;
import com.qk.management.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/courses")
@Slf4j
public class CourseController {
    @Autowired
    private CourseService courseService;
    @GetMapping("/subject/{subject}")
    public Result courlist(@PathVariable String subject){
        List<Course> courses = courseService.listByCourse(subject);
        return Result.success(courses);
    }
    @GetMapping("/list")
    public Result list(){
        List<CourseDTO.Data> courses = courseService.listAllCourses();
        return Result.success(courses);
    }

    @GetMapping
    public Result page(
            @RequestParam(name="name",required = false)String name,
            @RequestParam(name="subject",required = false)Integer subject,
            @RequestParam(name="target",required = false)Integer target,
            @RequestParam(name="page",defaultValue = "1")Integer page,
            @RequestParam(name="pageSize",defaultValue = "10")Integer pageSize){
        PageResult<Course> pageResult = courseService.pageCourse(name, subject, target, page, pageSize);
        log.info("分页查询课程,参数:课程名称={},课程状态={},当前页码={},每页大小={}", name, subject, page, pageSize);
        return Result.success(pageResult);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        log.info("删除课程：{}",id);
        courseService.deleteById(id);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Course course) {
        log.info("添加课程：{}",course);
        courseService.add(course);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        log.info("查询课程：{}",id);
        Course course = courseService.findById(id);
        return Result.success(course);
    }

    @PutMapping
    public Result update(@RequestBody Course course) {
        log.info("修改课程：{}",course);
        courseService.update(course);
        return Result.success();
    }
}
